<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('修改答题场次配置')" />
</head>
<body class="gray-bg">
	
	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-title">
						<h5>修改答题场次配置</h5>
					</div>
					<div class="ibox-content">
						<form class="form-horizontal m" id="form-config-edit" th:object="${examSessionConfig}">
							<input name="id" th:field="*{id}" type="hidden">
							<div class="form-group">
								<label class="col-sm-3 control-label">配置名称：</label>
								<div class="col-sm-8">
									<input name="configName" th:field="*{configName}" class="form-control" type="text" required>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">题目数量：</label>
								<div class="col-sm-8">
									<input name="questionCount" th:field="*{questionCount}" class="form-control" type="number" required>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">题目分类：</label>
								<div class="col-sm-8">
									<div class="input-group">
										<input type="hidden" name="categoryId" id="categoryId" th:field="*{categoryId}">
										<input class="form-control" type="text" id="categoryName" readonly>
										<span class="input-group-btn">
											<button type="button" class="btn btn-primary" onclick="selectCategory()"><i class="fa fa-search"></i> 选择</button>
										</span>
									</div>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">难度筛选：</label>
								<div class="col-sm-8">
									<select name="difficulty" th:field="*{difficulty}" class="form-control">
										<option value="0">全部</option>
										<option value="1">简单</option>
										<option value="2">中等</option>
										<option value="3">困难</option>
									</select>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">考试类型：</label>
								<div class="col-sm-8">
									<select name="examType" th:field="*{examType}" class="form-control" required>
										<option value="1">单人答题</option>
										<option value="2">PK答题</option>
									</select>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">考试时长：</label>
								<div class="col-sm-8">
									<input name="examDuration" th:field="*{examDuration}" class="form-control" type="number" required>
									<span class="help-block m-b-none">考试时间限制，单位：秒</span>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">状态：</label>
								<div class="col-sm-8">
									<select name="status" th:field="*{status}" class="form-control">
										<option value="0">禁用</option>
										<option value="1">启用</option>
									</select>
								</div>
							</div>
							<div class="form-group">
								<div class="form-control-static col-sm-offset-9">
									<button type="submit" class="btn btn-primary">提交</button>
									<button type="button" class="btn btn-danger" onclick="closeItem()">关闭</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
	
	<th:block th:include="include :: footer" />
	
	<script type="text/javascript">
		var prefix = ctx + "system/exam/config";
		
		$(function() {
			// 页面加载时显示当前分类名称
			var categoryId = $("#categoryId").val();
			if (categoryId && categoryId != "0") {
				// 通过AJAX获取分类名称
				$.ajax({
					url: ctx + "system/question/category/list",
					type: "post",
					data: {
						id: categoryId
					},
					success: function(result) {
						if (result.code == 0 && result.rows.length > 0) {
							$("#categoryName").val(result.rows[0].categoryName);
						} else {
							$("#categoryName").val("未知分类");
						}
					},
					error: function() {
						$("#categoryName").val("分类ID: " + categoryId);
					}
				});
			} else {
				$("#categoryName").val("全部分类");
			}
		});
		
		$("#form-config-edit").validate({
			rules: {
				configName: {
					required: true,
					maxlength: 50
				},
				questionCount: {
					required: true,
					digits: true,
					min: 1,
					max: 100
				},
				categoryId: {
					required: true,
					digits: true
				},
				difficulty: {
					required: true
				},
				examType: {
					required: true
				}
			},
			messages: {
				configName: {
					required: "请输入配置名称",
					maxlength: "配置名称不能超过50个字符"
				},
				questionCount: {
					required: "请输入题目数量",
					digits: "题目数量必须为整数",
					min: "题目数量不能少于1题",
					max: "题目数量不能超过100题"
				},
				categoryId: {
					required: "请选择题目分类",
					digits: "题目分类必须为整数"
				}
			},
			submitHandler: function(form) {
				$.operate.save(prefix + "/edit", $('#form-config-edit').serialize());
			}
		});
		
		// 选择题目分类
		function selectCategory() {
			// 打开题库分类选择窗口
			layer.open({
				type: 2,
				title: "选择题目分类",
				area: ['800px', '500px'],
				content: ctx + "system/question/category/select",
				btn: ['确定', '关闭'],
				yes: function(index, layero) {
					var iframeWin = layero.find('iframe')[0];
					var data = iframeWin.contentWindow.getSelected();
					if (data && data.length > 0) {
						$("#categoryId").val(data[0].id);
						$("#categoryName").val(data[0].categoryName);
					}
					layer.close(index);
				}
			});
		}
		
		function closeItem() {
			var index = parent.layer.getFrameIndex(window.name);
			parent.layer.close(index);
		}
	</script>
</body>
</html>